package org.example.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.commonapi.domain.CourseClass;
import org.example.commonapi.dto.CourseStudentDto;
import org.example.commonapi.dto.StudentDto;
import org.example.commonapi.mapper.CourseMapper;
import org.example.course.service.CourseClassService;
import org.example.commonapi.mapper.CourseClassMapper;
import org.example.course.until.CourseUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static org.example.commonapi.domain.Constants.NUMBER;

/**
* @author DELL
* @description 针对表【course_class(课程_班级表)】的数据库操作Service实现
* @createDate 2024-06-02 15:19:20
*/
@Service
public class CourseClassServiceImpl extends ServiceImpl<CourseClassMapper, CourseClass>
    implements CourseClassService{

    @Resource
    private CourseClassMapper courseClassMapper;


    /**
     * 创建课程班级
     * @param courseId
     * @param className
     */
    @Override
    public void insertClass(Long courseId, String className) {
        CourseClass courseClass = new CourseClass(courseId,className);
        courseClass.setInvite(CourseUntil.createInviteCode());
        courseClass.setNumber(NUMBER);
        courseClass.setCreateTime(LocalDateTime.now());
        courseClass.setUpdateTime(LocalDateTime.now());
        // 插入数据库
        courseClassMapper.insert(courseClass);
    }

    /**
     * 查询课程班级
     * @param courseId
     * @param className
     */
    @Override
    public List<CourseClass> courseClassList(Long courseId, String className) {
        // 判断是否存在
        LambdaQueryWrapper<CourseClass> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseClass::getCourseId,courseId);
        boolean isClassExist=courseClassMapper.exists(queryWrapper);
//        该课程存在班级
        if(isClassExist)return courseClassMapper.courseClassList(courseId,className);
//        不存在班级
        else return null;
    }

    /**
     * 查询该课程所有学生
     * @param courseId
     * @return
     */
    @Override
    public List<CourseStudentDto> courseClassStudentList(Long courseId) {
        return courseClassMapper.courseClassStudentList(courseId);
    }

}




